package com.yuantu.judiciary.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分页工具
 *
 * @author syw
 * @since 2023-10-17
 **/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PageUtil {

    public static <T> List<T> getPage(List<T> list, Pageable pageable) {
        return list.stream().skip((long) pageable.getPageNumber() * pageable.getPageSize())
                .limit(pageable.getPageSize())
                .collect(Collectors.toList());
    }


    public static <T> List<T> Page(List<T> list, Integer pageNum, Integer pageSize) {

        int beginIndex = 0;
        int endIndex = 0;
        beginIndex = pageSize * pageNum;
        endIndex = Math.min((pageNum + 1) * pageSize, list.size());
        if (beginIndex >= list.size()) {
            //超出范围则无数据
            list = new ArrayList<>();
        } else {
            endIndex = Math.min(list.size(), endIndex);
            list = list.subList(beginIndex, endIndex);
        }
        return list;
    }

}
